package ldh.im.fxgui.service;

import ldh.im.fxbase.dao.ImContentSearchDao;
import ldh.im.fxbase.data.dto.SearchResult;
import ldh.im.fxbase.data.dto.SearchResultGroup;
import ldh.im.fxbase.util.BeanFactory;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

/**
 * 内容查询
 */
public class ContentSearchService {

    private ImContentSearchDao contentSearchDao = BeanFactory.getInstance().getBean(ImContentSearchDao.class);

    public List<SearchResultGroup> search(String userId, String searchContent) throws SQLException {
        List<SearchResult> searchUserResult = searchUsers(searchContent);
        List<SearchResult> searchGroupResult = searchGroups(userId, searchContent);
        List<SearchResult> searchMessageResult = searchMessages(userId, searchContent);

        SearchResultGroup userResultGroup = new SearchResultGroup(searchUserResult.size(), searchUserResult);
        SearchResultGroup groupResultGroup = new SearchResultGroup(searchGroupResult.size(), searchGroupResult);
        SearchResultGroup messageResultGroup = new SearchResultGroup(searchMessageResult.size(), searchMessageResult);
        return Arrays.asList(userResultGroup, groupResultGroup, messageResultGroup);
    }

    /**
     * 查询用户
     * @return
     */
    public List<SearchResult> searchUsers(String searchContent) throws SQLException {
        return contentSearchDao.searchUsers(searchContent);
    }

    /**
     * 查询群用户和群
     * @return
     */
    public List<SearchResult> searchGroups(String userId, String searchContent) throws SQLException {
        return contentSearchDao.searchGroups(userId, searchContent);
    }

    /**
     * 查询消息
     * @return
     */
    public List<SearchResult> searchMessages(String userId, String searchContent) throws SQLException {
        return contentSearchDao.searchMessages(userId, searchContent);
    }
}
